﻿using LPP.Core.Services;
using LPP.Entity.Statistic;
using Microsoft.AspNetCore.Mvc;

namespace LPP.API.Controllers.StatisticsController
{
    [Route("api/[controller]")]
    [ApiController]
    public class StatisticsController : ControllerBase
    {
        private readonly IStatisticService _statisticService;

        public StatisticsController(IStatisticService statisticService)
        {
            _statisticService = statisticService;
        }

        [HttpGet("{key}")]
        public async Task<ActionResult<Statistic>> GetStatistic(string key)
        {
            var statistic = await _statisticService.GetStatisticAsync(key);
            if (statistic == null)
            {
                return NotFound();
            }
            return Ok(statistic);
        }

        [HttpPut("{key}")]
        public async Task<IActionResult> UpdateStatistic(string key, [FromBody] int value)
        {
            await _statisticService.UpdateStatisticAsync(key, value);
            return NoContent();
        }

        [HttpGet]
        public async Task<ActionResult<List<Statistic>>> GetAllStatistics()
        {
            return Ok(await _statisticService.GetAllStatisticsAsync());
        }
    }
}
